const app = getApp();
Component({
  data: {
    originFiles: [],
    gridConfig: {
      column: 4,
      width: 160,
      height: 160,
    },
    config: {
      count: 5,
      sourceType: ['album', 'camera'],
      camera: 'back',
    },
  },
  methods: {
    handleSuccess(e) {
      const { files } = e.detail;
      console.log(files)
      // this.setData({
      //   originFiles: files,
      // });
    },
    handleRemove(e) {
      const { index } = e.detail;
      const { originFiles } = this.data;
      originFiles.splice(index, 1);
      this.setData({
        originFiles,
      });
    },
    handleClick(e) {
      console.log(e.detail.file);
    },
    handleSelectChange(e) {
      const { currentSelectedFiles } = e.detail;
      this.onUploadSingle(currentSelectedFiles)
    },
    onUploadSingle(files) {
      var that = this;
      wx.showLoading({
        title: '正在上传中...',
        mask: true
      })
      let file = files[0][0]
      wx.uploadFile({
        url: app.globalData.uploadFile, // 仅为示例，非真实的接口地址
        filePath: file.path || file.url,
        name: 'file',
        formData: {},
        success: (res) => {
          // 上传成功
          wx.hideLoading()
          if (res.statusCode == 200) {
            let fileRealUrl = res.data;
            file.url = fileRealUrl;
            let originFilesTmp = that.data.originFiles;
            originFilesTmp.push(file);
            that.setData({
              originFiles: originFilesTmp,
            });
          }
        },
        fail: function (err) {
          console.log(err)
        },
        complete: res => {
          // console.log(res)
        }
      });
    },
  },

});
